package cn.ujn.medicalbackend.service.impl;

import cn.ujn.medicalbackend.common.ErrorCode;
import cn.ujn.medicalbackend.domain.*;

import cn.ujn.medicalbackend.exception.BusinessException;
import cn.ujn.medicalbackend.request.CityAddRequest;
import cn.ujn.medicalbackend.request.CityDeleteRequest;
import cn.ujn.medicalbackend.request.CityUpdateRequest;
import cn.ujn.medicalbackend.service.ChinaService;
import cn.ujn.medicalbackend.service.SysregionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ujn.medicalbackend.mapper.CityMapper;
import cn.ujn.medicalbackend.service.CityService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author Drink
* @description 针对表【city】的数据库操作Service实现
* @createDate 2024-10-26 09:34:50
*/
@Service
public class CityServiceImpl extends ServiceImpl<CityMapper, City>
    implements CityService{

    @Autowired
    private SysregionService sysregionService;
    @Autowired
    private ChinaService chinaService;
    @Override
    public Page<City> cityPageTransfer(Page<City> tempPage) {
        for (City city : tempPage.getRecords()) {
            long cityNumber = city.getCityNumber();
            Sysregion sysregion =sysregionService.getById(cityNumber);
            if (sysregion != null) {
                China china = chinaService.getById(sysregion.getParentId());
                if (china != null) {
                    city.setCity(sysregion.getName());
                    city.setProvince(china.getName());
                }
            }
        }
        return tempPage;
    }

    @Override
    public long addCity(CityAddRequest cityAddRequest, HttpServletRequest request) {
        City city = new City();
        BeanUtils.copyProperties(cityAddRequest, city);

        Date now = new Date();
        city.setCreatetime(now);
        city.setUpdatetime(now);
        // 3.校验信息
        if (city.getCityNumber() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "不能为空");
        }
        boolean result;
        result = save(city);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "保存失败");
        }

        return city.getCityId();
    }

    @Override
    public boolean updateCity(CityUpdateRequest cityUpdateRequest, HttpServletRequest request) {
        if (cityUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Date now = new Date();
        City city =getById(cityUpdateRequest.getCityId());
        if(city==null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "不存在，更新失败");
        }
        BeanUtils.copyProperties(cityUpdateRequest, city);
        city.setUpdatetime(now);

        boolean result =updateById(city);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "更新失败");
        }

        return true;
    }

    @Override
    public boolean deleteCity(CityDeleteRequest cityDeleteRequest, HttpServletRequest request) {
        Long cityId = cityDeleteRequest.getCityId();

        if (cityId == null ) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "；医生id和账户名不能为空");
        }
        QueryWrapper<City> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_id", cityId);
        long count = count(queryWrapper);
        if (count <= 0) {
            throw new BusinessException(ErrorCode.NULL_ERROR, "没有该药品");
        }

        boolean result = removeById(cityId);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "删除失败");
        }
        return true;
    }
}




